// The NSpring Framework for .NET
// Copyright (c) 2003, Jeffrey Varszegi
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met:
// 
// - Redistributions of source code must retain the above copyright notice, this 
// list of conditions and the following disclaimer.
// 
// - Redistributions in binary form must reproduce the above copyright notice, this 
// list of conditions and the following disclaimer in the documentation and/or other 
// materials provided with the distribution.
// 
// - Neither the name of the NSpring project nor the names of its contributors may 
// be used to endorse or promote products derived from this software without 
// specific prior written permission from the copyright owner.
// 
// - No product derived from this software may be called "NSpring", nor may 
// "NSpring" appear in the name of such a product, without specific prior written 
// permission from the copyright owner.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
// OF THE POSSIBILITY OF SUCH DAMAGE. 

namespace NSpring.Logging.Common.Pattern {

using System;
using System.Text;

using NSpring.Logging;

/// <summary>
/// Valid types of event property tokens
/// </summary>
internal enum EventPropertyType {
    LevelID,
    LevelName,
    LevelPriority,
    ID,
    Category,
    Code,
    Year,
    Month,
    Day,
    Hour,
    Minute,
    Second,
    Millisecond,
    Date,
    Time,
    Timestamp,
    TimeZone,
    MonthName,
    MonthShortName,
    Weekday,
    WeekdayName,
    WeekdayShortName,
    DayOfYear,
    WeekOfMonth,
    WeekOfYear
}

/// <summary>
/// <para>
/// Each EventPropertyToken is a pattern placeholder for a property of an Event object
/// All underlying pattern-related code is subject to change without warning
/// or deprecation, and should not be called directly by application code
/// </para>
/// </summary>
internal sealed class EventPropertyToken : Token {
    private static string timeZone                      = Constants.ISO_8601_TIME_ZONE;
    private static string daylightSavingsTimeZone       = Constants.ISO_8601_DAYLIGHT_SAVINGS_TIME_ZONE;
    private static string[] monthNames                  = Constants.MonthNames;
    private static string[] monthShortNames             = Constants.MonthShortNames;
    private static string[] weekdayNames                = Constants.WeekdayNames;
    private static string[] weekdayShortNames           = Constants.WeekdayShortNames;
    private static string[] yearStrings                 = Constants.YearStrings;
    private static string[] dateStrings                 = Constants.DateStrings;
    private static string[] millisecondStrings          = Constants.MillisecondStrings;

    private const char COLON_CHAR                       = ':';
    private const char HYPHEN_CHAR                      = '-';
    private const char PERIOD_CHAR                      = '.';
    private const char SPACE_CHAR                       = ' ';

    
    private const string BLANK_STRING                   = "";
    private const char SPACE                            = ' ';
    
    private Alignment alignment;
    private Case __case;
    private int minimumWidth;
    private int maximumWidth;
    
    private EventPropertyType propertyType;
    
    private EventPropertyToken() {}
    
    public EventPropertyToken(EventPropertyType _propertyType, string _text, int _minimumWidth, int _maximumWidth, Alignment _alignment, Case _case) {
        type = TokenType.EventProperty;
        propertyType = _propertyType;
        text = _text;
        minimumWidth = Math.Max(_minimumWidth, 0);
        maximumWidth = Math.Max(minimumWidth, _maximumWidth);
        alignment = _alignment;
        __case = _case;
    }
    
    public EventPropertyType PropertyType {
        get {
            return propertyType;
        }
    }

    public Alignment Alignment {
        get {
            return alignment;
        }
    }
    
    public Case Case {
        get {
            return __case;
        }
    }

    public int MinimumWidth {
        get {
            return minimumWidth;
        }
    }

    public int MaximumWidth {
        get {
            return maximumWidth;
        }
    }

    public override string Format(Event e) {
        string s = null;
        switch (propertyType) {
            case EventPropertyType.LevelID                 : {
                s = e.Level.IDString;
                break;
            }
            case EventPropertyType.LevelName                 : {
                s = e.Level.Name;
                break;
            }
            case EventPropertyType.LevelPriority             : {
                s = e.Level.PriorityString;
                break;
            }
            case EventPropertyType.ID                         : {
                s = e.ID;
                break;
            }
            case EventPropertyType.Category                   : {
                s = e.Category;
                break;
            }
            case EventPropertyType.Code                       : {
                s = e.Code;
                break;
            }
            case EventPropertyType.Year                       : {
                int year = e.Timestamp.Year;
                if (year < yearStrings.Length) {
                    return yearStrings[year];
                }
                else {
                    return year.ToString();
                }
            }
            case EventPropertyType.Month                      : {
                return dateStrings[e.Timestamp.Month];
            }
            case EventPropertyType.Day                        : {
                return dateStrings[e.Timestamp.Day];
            }
            case EventPropertyType.Hour                       : {
                return dateStrings[e.Timestamp.Hour];
            }
            case EventPropertyType.Minute                     : {
                return dateStrings[e.Timestamp.Minute];
            }
            case EventPropertyType.Second                     : {
                return dateStrings[e.Timestamp.Second];
            }
            case EventPropertyType.Millisecond                : {
                return millisecondStrings[e.Timestamp.Millisecond];
            }
            case EventPropertyType.Date                       : {
                StringBuilder sb = new StringBuilder(10);
                int year = e.Timestamp.Year;
                if (year < yearStrings.Length) {
                    sb.Append(yearStrings[year]).Append(HYPHEN_CHAR);
                }
                else {
                    sb.Append(year.ToString()).Append(HYPHEN_CHAR);
                }
                sb.Append(dateStrings[e.Timestamp.Month]).Append(HYPHEN_CHAR);
                sb.Append(dateStrings[e.Timestamp.Day]);
                return sb.ToString();
            }
            case EventPropertyType.Time                       : {
                StringBuilder sb = new StringBuilder(12);
                sb.Append(dateStrings[e.Timestamp.Hour]).Append(COLON_CHAR);
                sb.Append(dateStrings[e.Timestamp.Minute]).Append(COLON_CHAR);
                sb.Append(dateStrings[e.Timestamp.Second]).Append(PERIOD_CHAR);
                sb.Append(millisecondStrings[e.Timestamp.Millisecond]);
                return sb.ToString();
            }
            case EventPropertyType.Timestamp                  : {
                StringBuilder sb = new StringBuilder(23);
                int year = e.Timestamp.Year;
                if (year < yearStrings.Length) {
                    sb.Append(yearStrings[year]).Append(HYPHEN_CHAR);
                }
                else {
                    sb.Append(year.ToString()).Append(HYPHEN_CHAR);
                }
                sb.Append(dateStrings[e.Timestamp.Month]).Append(HYPHEN_CHAR);
                sb.Append(dateStrings[e.Timestamp.Day]).Append(SPACE_CHAR);
                sb.Append(dateStrings[e.Timestamp.Hour]).Append(COLON_CHAR);
                sb.Append(dateStrings[e.Timestamp.Minute]).Append(COLON_CHAR);
                sb.Append(dateStrings[e.Timestamp.Second]).Append(PERIOD_CHAR);
                sb.Append(millisecondStrings[e.Timestamp.Millisecond]);
                return sb.ToString();
            }
            case EventPropertyType.TimeZone                  : {
                switch (e.Timestamp.Month) {
                    case 1       :
                    case 2       :
                    case 3       : {
                        return daylightSavingsTimeZone;
                    }
                    case 4       : {
                        if (e.Timestamp.Day > 2) {
                            return timeZone;
                        }
                        else {
                            return daylightSavingsTimeZone;
                        }
                    }
                    case 5       :
                    case 6       :
                    case 7       :
                    case 8       :
                    case 9       : {
                        return timeZone;
                    }
                    case 10      : {
                        if (e.Timestamp.Day < 30) {
                            return timeZone;
                        }
                        else {
                            return daylightSavingsTimeZone;
                        }
                    }
                    case 11      :
                    case 12      : {
                        return daylightSavingsTimeZone;
                    }
                    default      : {
                        return timeZone;
                    }
                }
            }
            case EventPropertyType.MonthName                 : {
                s = monthNames[e.Timestamp.Month];
                break;
            }
            case EventPropertyType.MonthShortName           : {
                s = monthShortNames[e.Timestamp.Month];
                break;
            }
            case EventPropertyType.Weekday                    : {
                return (((int)e.Timestamp.DayOfWeek) + 1).ToString();
            }
            case EventPropertyType.WeekdayName               : {
                s = weekdayNames[(int)e.Timestamp.DayOfWeek];
                break;
            }
            case EventPropertyType.WeekdayShortName         : {
                s = weekdayShortNames[(int)e.Timestamp.DayOfWeek];
                break;
            }
            case EventPropertyType.DayOfYear                : {
                return millisecondStrings[e.Timestamp.DayOfYear];
            }
            case EventPropertyType.WeekOfMonth              : {
                return ((e.Timestamp.Day / 7) + 1).ToString();
            }
            case EventPropertyType.WeekOfYear               : {
                return dateStrings[(e.Timestamp.DayOfYear / 7) + 1];
            }
        }
        if (s == null) {
            if (minimumWidth == 0) {
                s = BLANK_STRING;
            }
            else {
                s = BLANK_STRING.PadLeft(minimumWidth, SPACE);
            }
        }
        else {
            if (s.Length < minimumWidth) {
                switch (alignment) {
                    case Alignment.Center   : {
                        int leftMargin = ((minimumWidth - s.Length) / 2);
                        if (leftMargin > 0) {
                            s = s.PadLeft((s.Length + leftMargin), SPACE);
                        }
                        s = s.PadRight(minimumWidth, SPACE);
                        break;
                    }
                    case Alignment.Left     : {
                        s = s.PadRight(minimumWidth, SPACE);
                        break;
                    }
                    case Alignment.Right    : {
                        s = s.PadLeft(minimumWidth, SPACE);
                        break;
                    }
                    case Alignment.Default  : {
                        s = s.PadRight(minimumWidth, SPACE);
                        break;
                    }
                }
            }
            else if (s.Length > maximumWidth) {
                s = s.Substring(0, maximumWidth);
            }
        }
        switch (__case) {
            case Case.Lower     : {
                s = s.ToLower();
                break;
            }
            case Case.Upper     : {
                s = s.ToUpper();
                break;
            }
            case Case.Default   : {
                break;
            }
        }
        return s;
    }
    
    public override void Format(Event e, StringBuilder sb) {
        string s = null;
        switch (propertyType) {
            case EventPropertyType.LevelID                   : {
                s = e.Level.IDString;
                break;
            }
            case EventPropertyType.LevelName                 : {
                s = e.Level.Name;
                break;
            }
            case EventPropertyType.LevelPriority             : {
                s = e.Level.PriorityString;
                break;
            }
            case EventPropertyType.ID                         : {
                s = e.ID;
                break;
            }
            case EventPropertyType.Category                   : {
                s = e.Category;
                break;
            }
            case EventPropertyType.Code                       : {
                s = e.Code;
                break;
            }
            case EventPropertyType.Year                       : {
/*
                int year = e.Timestamp.Year;
                if (year < yearStrings.Length) {
                    sb.Append(yearStrings[year]);
                }
                else {
                    sb.Append(year.ToString());
                }
*/
                sb.Append(yearStrings[e.Timestamp.Year]);
                return;
            }
            case EventPropertyType.Month                      : {
                sb.Append(dateStrings[e.Timestamp.Month]);
                return;
            }
            case EventPropertyType.Day                        : {
                sb.Append(dateStrings[e.Timestamp.Day]);
                return;
            }
            case EventPropertyType.Hour                       : {
                sb.Append(dateStrings[e.Timestamp.Hour]);
                return;
            }
            case EventPropertyType.Minute                     : {
                sb.Append(dateStrings[e.Timestamp.Minute]);
                return;
            }
            case EventPropertyType.Second                     : {
                sb.Append(dateStrings[e.Timestamp.Second]);
                return;
            }
            case EventPropertyType.Millisecond                : {
                sb.Append(millisecondStrings[e.Timestamp.Millisecond]);
                return;
            }
            case EventPropertyType.Date                       : {
/*
                int year = e.Timestamp.Year;
                if (year < yearStrings.Length) {
                    sb.Append(yearStrings[year]).Append(HYPHEN_CHAR);
                }
                else {
                    sb.Append(year.ToString()).Append(HYPHEN_CHAR);
                }
*/
                sb.Append(yearStrings[e.Timestamp.Year]).Append(HYPHEN_CHAR);
                sb.Append(dateStrings[e.Timestamp.Month]).Append(HYPHEN_CHAR);
                sb.Append(dateStrings[e.Timestamp.Day]);
                return;
            }
            case EventPropertyType.Time                       : {
                sb.Append(dateStrings[e.Timestamp.Hour]).Append(COLON_CHAR);
                sb.Append(dateStrings[e.Timestamp.Minute]).Append(COLON_CHAR);
                sb.Append(dateStrings[e.Timestamp.Second]).Append(PERIOD_CHAR);
                sb.Append(millisecondStrings[e.Timestamp.Millisecond]);
                return;
            }
            case EventPropertyType.Timestamp                  : {
/*
                int year = e.Timestamp.Year;
                if (year < yearStrings.Length) {
                    sb.Append(yearStrings[year]).Append(HYPHEN_CHAR);
                }
                else {
                    sb.Append(year.ToString()).Append(HYPHEN_CHAR);
                }
*/
                sb.Append(yearStrings[e.Timestamp.Year]).Append(HYPHEN_CHAR);
                sb.Append(dateStrings[e.Timestamp.Month]).Append(HYPHEN_CHAR);
                sb.Append(dateStrings[e.Timestamp.Day]).Append(SPACE_CHAR);
                sb.Append(dateStrings[e.Timestamp.Hour]).Append(COLON_CHAR);
                sb.Append(dateStrings[e.Timestamp.Minute]).Append(COLON_CHAR);
                sb.Append(dateStrings[e.Timestamp.Second]).Append(PERIOD_CHAR);
                sb.Append(millisecondStrings[e.Timestamp.Millisecond]);
                return;
            }
            case EventPropertyType.TimeZone                  : {
                switch (e.Timestamp.Month) {
                    case 1       :
                    case 2       :
                    case 3       : {
                        sb.Append(daylightSavingsTimeZone);
                        return;
                    }
                    case 4       : {
                        if (e.Timestamp.Day > 2) {
                            sb.Append(timeZone);
                            return;
                        }
                        else {
                            sb.Append(daylightSavingsTimeZone);
                            return;
                        }
                    }
                    case 5       :
                    case 6       :
                    case 7       :
                    case 8       :
                    case 9       : {
                        sb.Append(timeZone);
                        return;
                    }
                    case 10      : {
                        if (e.Timestamp.Day < 30) {
                            sb.Append(timeZone);
                            return;
                        }
                        else {
                            sb.Append(daylightSavingsTimeZone);
                            return;
                        }
                    }
                    case 11      :
                    case 12      : {
                        sb.Append(daylightSavingsTimeZone);
                        return;
                    }
                    default      : {
                        sb.Append(timeZone);
                        return;
                    }
                }
            }
            case EventPropertyType.MonthName                 : {
                s = monthNames[e.Timestamp.Month];
                break;
            }
            case EventPropertyType.MonthShortName           : {
                s = monthShortNames[e.Timestamp.Month];
                break;
            }
            case EventPropertyType.Weekday                    : {
                sb.Append(((int)e.Timestamp.DayOfWeek) + 1);
                return;
            }
            case EventPropertyType.WeekdayName               : {
                s = weekdayNames[(int)e.Timestamp.DayOfWeek];
                break;
            }
            case EventPropertyType.WeekdayShortName         : {
                s = weekdayShortNames[(int)e.Timestamp.DayOfWeek];
                break;
            }
            case EventPropertyType.DayOfYear                : {
                sb.Append(millisecondStrings[e.Timestamp.DayOfYear]);
                return;
            }
            case EventPropertyType.WeekOfMonth              : {
                sb.Append((e.Timestamp.Day / 7) + 1);
                return;
            }
            case EventPropertyType.WeekOfYear               : {
                sb.Append(dateStrings[(e.Timestamp.DayOfYear / 7) + 1]);
                return;
            }
        }
        if (s == null) {
            if (minimumWidth == 0) {
                return;
            }
            else {
                sb.Append(BLANK_STRING.PadLeft(minimumWidth, SPACE));
                return;
            }
        }
        else {
            int sl = s.Length;
            if (sl < minimumWidth) {
                switch (alignment) {
                    case Alignment.Center   : {
                        int leftMargin = ((minimumWidth - sl) / 2);
                        if (leftMargin > 0) {
                            s = s.PadLeft((sl + leftMargin), SPACE);
                        }
                        s = s.PadRight(minimumWidth, SPACE);
                        break;
                    }
                    case Alignment.Left     : {
                        s = s.PadRight(minimumWidth, SPACE);
                        break;
                    }
                    case Alignment.Right    : {
                        s = s.PadLeft(minimumWidth, SPACE);
                        break;
                    }
                    case Alignment.Default  : {
                        s = s.PadRight(minimumWidth, SPACE);
                        break;
                    }
                }
            }
            else if (sl > maximumWidth) {
                s = s.Substring(0, maximumWidth);
            }
        }
        switch (__case) {
            case Case.Lower     : {
                sb.Append(s.ToLower());
                return;
            }
            case Case.Upper     : {
                sb.Append(s.ToUpper());
                return;
            }
            case Case.Default   : {
                sb.Append(s);
                return;
            }
        }
    }
    
    private static string GetLocalTimeZone(int month, int day) {
        switch (month) {
            case 1       :
            case 2       :
            case 3       : {
                return daylightSavingsTimeZone;
            }
            case 4       : {
                if (day > 2) {
                    return timeZone;
                }
                else {
                    return daylightSavingsTimeZone;
                }
            }
            case 5       :
            case 6       :
            case 7       :
            case 8       :
            case 9       : {
                return timeZone;
            }
            case 10      : {
                if (day < 30) {
                    return timeZone;
                }
                else {
                    return daylightSavingsTimeZone;
                }
            }
            case 11      :
            case 12      : {
                return daylightSavingsTimeZone;
            }
            default      : {
                return timeZone;
            }
        }
    }

}

}
